import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from tools.base_page import BasePage


class Kaobian(BasePage):

    def select_staffing(self):  # 选编制
        select_staffing = ("text", "选编制")
        sydanwei = ("text", "事业单位")
        guoqi = ("text", "国企")
        gyyh = ("text", "国有银行")
        teacher = ('text', '事业编教师')
        syb_yl = ('text', '事业编医疗')
        jdwz = ('text', '军队文职')
        jcxm = ('text', '基层项目')

        type_locator_list = (sydanwei, guoqi, gyyh, teacher, syb_yl, jdwz, jcxm)
        self.find_element(select_staffing).click()
        self.staffing_type_choose(type_locator_list)

    def staffing_type_choose(self, type_locator_list):  # 编制按照分类选择
        for num in range(0, 7):
            article_first = ("id", "com.znb.zxx:id/comm_item_news_title")
            self.find_element(type_locator_list[num]).click()
            self.elements(article_first)[0].click()
            time.sleep(2)
            self.driver.back()
        self.page(self.index)

    def unit_presentation(self):  # 单位介绍
        dwjs = ('text', '单位介绍')
        first_unit = ('id', 'com.znb.zxx:id/item_work_info_title')
        rbfs = ('text', '入编方式')
        study_course = ('text', '学习课程')
        firse_course_title = ('id', 'com.znb.zxx:id/comm_item_course_title')
        school_detail = ('id', 'workDescribeTV')

        self.find_element(dwjs).click()
        self.elements(first_unit)[0].click()
        self.find_element(rbfs).click()
        self.find_element(study_course).click()
        self.elements(firse_course_title)[0].click()
        time.sleep(2)
        self.driver.back()
        self.find_element(school_detail).click()
        self.driver.back()
        self.page(self.index)

    def unit_type_choose(self):  # 单位按照分类选择
        dwjs = ('text', '单位介绍')
        self.find_element(dwjs).click()
        for num in range(2, 7):
            # 左侧分类
            unit_classify = (
            'xpath', '//*[@resource-id="com.znb.zxx:id/workSortRv"]/android.view.ViewGroup[{}]'.format(num))
            self.find(unit_classify).click()
            for i in range(1, 3):
                unit_tpye_locator = (
                'xpath', '//*[@resource-id="com.znb.zxx:id/workSortChildRv"]/android.view.ViewGroup[{}]'.format(i))
                self.find(unit_tpye_locator).click()
                time.sleep(2)
        self.page(self.index)

    def seach_unit(self, text_value_list):
        '''

        :param text_value_list: 要搜索的关键字
        :return:
        '''
        seach_locator = ('id', 'searchIV')
        search_edit_text = ('id', 'search_edit_text')
        first_item_title = ('id', 'com.znb.zxx:id/item_title')
        dwjs = ('text', '单位介绍')
        self.find_element(dwjs).click()
        self.find(seach_locator).click()
        for item in range(2):
            self.find(search_edit_text).send_keys(text_value_list[item])
            self.elements(first_item_title)[0].click()
            time.sleep(1)
            self.back_activity(1)

        self.page(self.index)

    def public_notice(self):  # 公职公告
        gzgg = ('text', '公职公告')
        first_notice = ('id', 'com.znb.zxx:id/item_bulletin_title')
        public_list = ('text', '职位列表')
        first_public_title = ('xpath', '//*[@resource-id="com.znb.zxx:id/recyclerView"]/android.view.ViewGroup[1]')
        self.find_element(gzgg).click()
        self.elements(first_notice)[0].click()
        for num in range(2):
            self.go_swipe(0.5, 0.9, 0.5, 0.1)
        self.find_element(public_list).click()
        ele = self.clickable_or_not(first_public_title)
        if ele:
            ele.click()
            self.page(self.index)
        else:
            self.page(self.index)

    def kb_news(self):
        kaobian_news = ('text', '考编资讯')
        news_first = ('id', 'com.znb.zxx:id/comm_item_news_title')
        self.find_element(kaobian_news).click()
        self.elements(news_first)[0].click()
        self.page(self.index)
        return True

    def kb_open_course(self):
        courese_list_title = ('xpath',
                              '//*[@class="androidx.recyclerview.widget.RecyclerView"]/android.view.ViewGroup[1]/android.widget.TextView[1]')  # 课程列表中的课程标题
        cat_more_open_course = (
            'xpath', '//*[contains(@resource-id,"com.znb.zxx:id/contentTv") and contains(@text,"查看更多公开课")]')
        self.find(cat_more_open_course).click()
        try:
            self.find_by_scroll('免费领取').click()
        except:
            self.find(courese_list_title).click()
        self.page(self.index)

    def add_kb_target(self):
        # 添加考编目标
        add_locator = ('text', '添加')
        time.sleep(2)
        self.go_swipe(0.5, 0.6, 0.5, 0.4)
        myOfficialEnterIv = ('id', 'myOfficialEnterIv')  # 添加目标箭头
        setout_address = ('id', 'choiceTargetTv')  # 未添加过考编目标用户第一次进，备考地区按钮
        area = ('id', 'areaContent')  # 地区
        area_del = ('id', 'item_area_del')

        self.find(myOfficialEnterIv).click()
        if self.is_element(setout_address) == True:
            self.add_address_list()
            self.no_target_added()
            return True
        self.find(area).click()
        eles = self.elements(area_del)
        for i in range(len(eles)):
            eles[0].click()
        self.find_element(('text', '确定')).click()

    def add_address_list(self):
        '''
        添加四个备考地区
        '''
        area = ('id', 'areaContent')  # 地区
        setout_address = ('id', 'choiceTargetTv')  # 未添加过考编目标用户第一次进，备考地区按钮
        right_address = ('xpath', '//*[@resource-id="com.znb.zxx:id/cityRecyclerView"]/android.view.ViewGroup[1]')
        add_locator = ('text', '添加')

        for num in range(2, 6):
            left_address = (
                'xpath',
                '//*[@resource-id="com.znb.zxx:id/provinceRecyclerView"]/android.view.ViewGroup[{}]'.format(num))
            if self.is_element(area) == False:
                self.find(setout_address).click()
            else:
                self.find_element(area).click()
            self.find_element(add_locator).click()
            self.find(left_address).click()
            self.find(right_address).click()
            if num in [2, 3, 4]:
                self.back_activity(1)
            elif num == 5:
                try:
                    pass
                except:
                    self.find_element(('text', '下一步')).click()

    def no_target_added(self):
        '''
        添加编制类型。
        :return:
        '''
        finish = ('text', '完成')
        bz_tpye = ('id', 'choiceTargetTv')
        next_locator = ('text', '下一步')
        notarize = ('text', '确认')
        first_bz_type = ('id', 'officialFirst')  # 首选编制类型
        two_bz_type = ('id', 'officialSecond')  # 次选编制类型
        if self.is_element(first_bz_type) == True:
            self.find(first_bz_type).click()
            self.find_element(finish).click()
            self.find(two_bz_type).click()
        else:
            self.find_element(next_locator).click()
            self.find(bz_tpye).click()
            self.find_element(finish).click()
            self.find_element(next_locator).click()
            self.find(bz_tpye).click()
        self.go_swipe(0.3, 0.7, 0.3, 0.6)
        self.find_element(finish).click()
        self.find_element(notarize).click()

    def verify_targe_add_succeed(self):
        '''
        验证编制目标添加成功
        :return:
        '''
        kb_address = ('id', 'myOfficialSubTitle')
        self.go_swipe(0.5, 0.7, 0.5, 0.3)
        if self.find(kb_address).text == '河北、山西、天津市、北京':
            print('考编目标添加成功')
            self.page(self.index)
            return True
        else:
            return False

    def kb_job(self):
        job_title = ('id', 'item_bulletin_job_title')
        job_list = ('id', 'tab_title')
        job_list_detail = ('id', 'item_job_title')

        self.elements(job_title)[0].click()
        self.find_by_scroll('查看公告详情').click()
        self.go_swipe(0.5, 0.8, 0.5, 0.2)
        self.elements(job_list)[1].click()
        self.elements(job_list_detail)[1].click()
        self.page(self.index)

    def kb_notice(self):
        first_notice_title = ('id', 'item_bulletin_title')
        time.sleep(2)
        self.go_swipe(0.5, 0.9, 0.5, 0.1)
        self.find_element(('text', "公告")).click()
        self.elements(first_notice_title)[0].click()
        self.page(self.index)

    def kb_paper(self):
        notarize = ('id', 'dialog_warn_confirm')
        time.sleep(5)
        self.go_swipe(0.5, 0.9, 0.5, 0.1)
        self.find_by_scroll('真题').click()
        for i in range(1, 10):
            locator = ('xpath', '//*[@resource-id="com.znb.zxx:id/recyclerView"]/android.view.ViewGroup[{}]'.format(i))
            location = ("id", "examStartBtn")
            text_name = '开始做题'
            text_value = '重新做题'
            if self.find_locator_and_back(locator, location, text_name, location, text_value) == True:
                break

        self.back_activity(1)
        time.sleep(1)
        self.find(notarize).click()
        self.page(self.index)
